/*
包名称：用户 满意度调查数据统计
关联模板：myddcsjtj.html
*/
package controllers

import (
	"net/http"
	"time"
	"xmgl/models"

	"log"

	"github.com/astaxie/beego"
)

type MyddcsjtjController struct {
	beego.Controller
}

func (this *MyddcsjtjController) Get() {
	var (
		menu models.Menu_gl
		user models.Sut_user
	)
	if this.GetSession("USER") == nil {
		//没有取到session值，页面跳转
		http.Redirect(this.Ctx.ResponseWriter, this.Ctx.Request, "/login", http.StatusFound)
	}
	tmp, ok := this.GetSession("USER").(models.Sut_user) //注意类型断言
	if ok {
		user = tmp
	}
	//log.Println("从session取用户名：", tmp.User_name, "ID：", tmp.User_id)
	//sess := this.StartSession()
	//sess.Set("USER", user)
	//log.Println("从session取用户名：", sess.Get("USER"))

	this.Data["WEBSITE"] = "运维管理/多媒体教室满意度调查数据统计与分析"
	this.Data["MENU_LEFT"] = menu.Get_left_menu()
	this.Data["MENU_RIGHT"] = menu.Get_right_menu()
	this.Data["USER_NAME"] = user.User_name

	this.Layout = "layout.html"
	this.TplName = "myddcsjtj.html"
	this.LayoutSections = make(map[string]string)
	this.LayoutSections["HtmlHeader"] = "html_header.html"
}

type Api_MyddcsjtjController struct {
	beego.Controller
}

func (this *Api_MyddcsjtjController) Get() {
	//测试：http://127.0.0.1//api/v1/wwfw/myddcsjtj/1?start_date=1493596800&end_date=1496989877
	var (
		api_id string
		data1  models.Stu_Myddc_Data1
		data2  models.Stu_Myddc_Data2
		err    error
	)
	api_id = this.GetString(":api_id", "")
	//log.Println("api_id:", api_id)
	if api_id == "1" { //区域图表数据
		//log.Println("API的id为1，调用取图表参数1！")
		var (
			start_date_unix int64
			end_date_unix   int64
			start_date      time.Time
			end_date        time.Time
		)
		start_date_unix, err = this.GetInt64("start_date") //取得API调用时，AJAX传来的UNIX时间值
		if err != nil {
			log.Println("读取javascript传来的unix时间到start_date_unix时出错！err:", err)
			this.Data["json"] = data1
			this.ServeJSON()
		}
		end_date_unix, err = this.GetInt64("end_date") //取得API调用时，AJAX传来的UNIX时间值
		if err != nil {
			log.Println("读取javascript传来的unix时间到start_date_unix时出错！err:", err)
			this.Data["json"] = data1
			this.ServeJSON()
		}
		//log.Println("读取传入参数，start_date:", start_date_unix, "\t end_date:", end_date_unix)
		start_date = time.Unix(start_date_unix, 0) //将unix时间转成Time结构
		end_date = time.Unix(end_date_unix, 0)     //将unix时间转成Time结构

		data1.Get_TuBiao1(start_date, end_date)
		this.Data["json"] = data1
	} else if api_id == "2" { //关系图表数据
		//log.Println("API的id为2，调用取图表参数2！")
		var start_year int
		data2.Get_TuBiao2(start_year)
		this.Data["json"] = data2
	}
	this.ServeJSON()
}
